package com.example.detectivedemo.domain.player;

import java.util.Optional;
import java.util.UUID;

/**
 * 玩家服务类
 * 实现玩家相关的业务逻辑
 */
public class PlayerService {
    
    private final PlayerRepository playerRepository;
    
    public PlayerService(PlayerRepository playerRepository) {
        this.playerRepository = playerRepository;
    }
    
    /**
     * 玩家注册
     * @param username 用户名
     * @param nickname 昵称
     * @return 注册成功的玩家对象
     * @throws IllegalArgumentException 如果用户名已存在
     */
    public Player register(String username, String nickname) {
        // 检查用户名是否已存在
        Optional<Player> existingPlayer = playerRepository.findByUsername(username);
        if (existingPlayer.isPresent()) {
            throw new IllegalArgumentException("用户名已存在");
        }
        
        // 创建新玩家
        String playerId = UUID.randomUUID().toString();
        Player newPlayer = new Player(playerId, username, nickname);
        
        // 保存玩家信息
        return playerRepository.create(newPlayer);
    }
    
    /**
     * 玩家登录
     * @param username 用户名
     * @return 登录成功的玩家对象
     * @throws IllegalArgumentException 如果用户名不存在
     */
    public Player login(String username) {
        // 查找玩家
        Optional<Player> playerOptional = playerRepository.findByUsername(username);
        if (!playerOptional.isPresent()) {
            throw new IllegalArgumentException("用户名不存在");
        }
        
        Player player = playerOptional.get();
        player.setLogin(true);
        
        // 保存登录状态
        playerRepository.update(player);
        
        return player;
    }
    
    /**
     * 玩家退出登录
     * @param playerId 玩家ID
     * @return 是否退出成功
     */
    public boolean logout(String playerId) {
        Optional<Player> playerOptional = playerRepository.findById(playerId);
        if (playerOptional.isPresent()) {
            Player player = playerOptional.get();
            player.setLogin(false);
            playerRepository.update(player);
            return true;
        }
        return false;
    }
    
    /**
     * 获取玩家信息
     * @param playerId 玩家ID
     * @return 玩家对象
     */
    public Optional<Player> getPlayerInfo(String playerId) {
        return playerRepository.findById(playerId);
    }
    
    /**
     * 更新玩家昵称
     * @param playerId 玩家ID
     * @param newNickname 新昵称
     * @return 更新后的玩家对象
     * @throws IllegalArgumentException 如果玩家不存在
     */
    public Player updateNickname(String playerId, String newNickname) {
        Optional<Player> playerOptional = playerRepository.findById(playerId);
        if (!playerOptional.isPresent()) {
            throw new IllegalArgumentException("玩家不存在");
        }
        
        Player player = playerOptional.get();
        player.setNickname(newNickname);
        
        return playerRepository.update(player);
    }
    
    /**
     * 玩家完成案件
     * @param playerId 玩家ID
     * @param caseScore 案件得分
     * @return 更新后的玩家对象
     * @throws IllegalArgumentException 如果玩家不存在
     */
    public Player completeCase(String playerId, int caseScore) {
        Optional<Player> playerOptional = playerRepository.findById(playerId);
        if (!playerOptional.isPresent()) {
            throw new IllegalArgumentException("玩家不存在");
        }
        
        Player player = playerOptional.get();
        
        // 增加解决案件数和得分
        player.addSolvedCase(caseScore);
        
        // 增加经验值（简单规则：得分的10%作为经验）
        int exp = (int) (caseScore * 0.1);
        player.addExperience(exp);
        
        // 保存玩家状态
        playerRepository.update(player);
        playerRepository.savePlayerState(player);
        
        return player;
    }
    
    /**
     * 加载玩家状态
     * @param playerId 玩家ID
     * @return 玩家对象
     */
    public Optional<Player> loadPlayerState(String playerId) {
        return playerRepository.loadPlayerState(playerId);
    }
}